package com.baikeyang.app.controller.admin;

import com.baikeyang.common.bean.PageResult;
import com.baikeyang.common.model.SystemLog;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.service.logs.ISystemLogService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by lenovo on 2018/7/13.
 */
@RestController(value = "loginLogController")
@RequestMapping(value = "/admin/log")
public class LoginLogController {


	@Autowired
	private ISystemLogService systemLogService;

	@RequestMapping(value = "index")
	public ModelAndView index () {
		return new ModelAndView("/admin/logs/log-index");
	}


	@RequestMapping(value = "list")
	public Object getSysLogs (String page, String recPerPage, String search, String sortBy, String order) {
		//list?page=0&recPerPage=10&sortBy=&order=asc
		PageResult pagers = new PageResult();
		try{
			Integer size = 10;
			Integer current = 1;
			if(StringUtils.isNotEmpty(recPerPage)){
				try {
					size = Integer.parseInt(recPerPage);
				} catch (Exception e) {}
			}
			if(StringUtils.isNotEmpty(page)) {
				try {
					current = Integer.parseInt(page);
					if(current < 1){current = 1;}
				} catch (Exception e) {}
			}
			Page<SystemLog> pageParam = new Page<>(current, size);
			//Page<User> pageParam = new Page<User>();
//            pageParam.setSize(size);
//            pageParam.setCurrent(current);
			IPage<SystemLog> pageResult = systemLogService.getSystemLogsPage(pageParam);
			pagers.setData(pageResult.getRecords());
			pagers.setPager(current, pageResult.getTotal());
			pagers.setResult("success");
		}catch (Exception e) {
			e.printStackTrace();
		}
		return pagers;
	}

}
